﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.DataDictionary.Agents;
using Sector7.Framework.Consts;
using Sector7.Framework.Enums;

namespace Sector7.Framework.DataDictionary.Windows
{
    /// <summary>
    /// AddMetadataWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddMetadataWindow : AddModeWindowBase
    {
        private List<S7_DataGroup> m_groups = null;

        public AddMetadataWindow()
        {
            InitializeComponent();
        }

        public S7_Metadata Metadata
        {
            get { return m_entity as S7_Metadata; }
            set { m_entity = value; }
        }

        protected override void InitializeForCreating()
        {
            txbMetadataId.Text = string.Empty;
            //cbbElements.SelectedItem = null;
            txbColumnName.Text = string.Empty;

            m_entity = new S7_Metadata();
        }

        protected override void InitializeForUpdating()
        {
            btnContinue.Visibility = Visibility.Hidden;
        }

        protected override bool CheckData()
        {
            m_errors.Clear();

            var selectedGroup = cbbGroups.SelectedItem as S7_DataGroup;
            Metadata.MetadataId = txbMetadataId.Text;
            var selectedElement = cbbElements.SelectedItem as S7_DataElement;
            Metadata.ColumnName = txbColumnName.Text;

            if (selectedGroup == null)
            {
                m_errors.Add("数据分组", "不能为空！");
            }
            else
            {
                Metadata.GroupId = selectedGroup.GroupId;
            }

            if (string.IsNullOrEmpty(Metadata.MetadataId))
            {
                m_errors.Add("元数据标识", "不能为空！");
            }

            if (selectedElement == null)
            {
                m_errors.Add("数据元", "不能为空！");
            }
            else
            {
                Metadata.ElementId = selectedElement.ElementId;
            }
            if (string.IsNullOrEmpty(Metadata.ColumnName))
            {
                m_errors.Add("列名", "不能为空！");
            }

            return base.CheckData();
        }

        protected override void AddData()
        {
            //内部标示符
            Metadata.MetadataId = txbMetadataId.Text; 
            //Metadata.ElementId = cbbElements.SelectedValue.ToString();
            //数据元标示符
            Metadata.ElementId = (cbbElements.SelectedItem as S7_DataElement).ElementId.ToString();
            //数据元名称
            Metadata.ElementName = (cbbElements.SelectedItem as S7_DataElement).ElementName.ToString();
            //数据类型
            Metadata.ValueType = new S7_ValueType();
            Metadata.ValueType.TypeIdentifier = (cbbElements.SelectedItem as S7_DataElement).ValueType.TypeIdentifier.ToString();
            //列名
            Metadata.ColumnName = txbColumnName.Text;

            DataTemplateServiceAgent.AddMetadata(Metadata);

            base.AddData();
        }

        protected override void GetData()
        {
            m_groups = DataGroupServiceAgent.GetAll(C_DataGroupType.DATA_ELEMENT);
            cbbGroups.ItemsSource = m_groups;

            if (m_groups != null && m_groups.Count > 0)
            {
                cbbGroups.SelectedIndex = 0;
            }
            else
            {
                cbbGroups.SelectedIndex = -1;
            }
        }

        private void cbbGroups_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedGroup = cbbGroups.SelectedItem as S7_DataGroup;

            if (selectedGroup.Items == null)
            {
                selectedGroup.Items = new List<object>();
                selectedGroup.Items.AddRange(DataElementServiceAgent.GetAll(selectedGroup.GroupId));
            }

            cbbElements.ItemsSource = selectedGroup.Items;
        }

        private void cbbElements_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Up && e.Key != Key.Down && e.Key != Key.Enter)
            {
                string keyword = cbbElements.Text;
                var group = cbbGroups.SelectedItem as S7_DataGroup;

                if (string.IsNullOrEmpty(keyword))
                {
                    cbbElements.ItemsSource = group.Items;
                }
                else
                {
                    var filteredList = group.Items.FindAll(delegate(object __element)
                    {
                        string elementId = (__element as S7_DataElement).ElementId;

                        return elementId.Contains(keyword);
                    });

                    cbbElements.ItemsSource = filteredList;
                    cbbElements.Items.Refresh();
                }
            }
        }

        protected override void DoNew()
        {
            var addDataElementWindow = new AddDataElementWindow();
            
            //将数据分组选中值传递到AddDataElementWindow中
            S7_DataGroup m_dataGroup = new S7_DataGroup();
            m_dataGroup.GroupIdentifier = (cbbGroups.SelectedItem as S7_DataGroup).GroupIdentifier.ToString();
            m_dataGroup.GroupName = (cbbGroups.SelectedItem as S7_DataGroup).GroupName.ToString();
            addDataElementWindow.DataGroup = m_dataGroup;

            addDataElementWindow.InitializeWindow(CrudType.Create);

            addDataElementWindow.ShowDialog();

            base.DoNew();
        }

        private void btn_AddDataElementWindow_Click(object sender, RoutedEventArgs e)
        {
            DoNew();
        }

        private void cbbElements_GotFocus(object sender, RoutedEventArgs e)
        {
            cbbElements.IsDropDownOpen = true;
        }
    }
}
